package RobotPlayer
{
	import BasicEngine.Vector2D;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	/**
	 * 机器模块
	 * 有机器零件组成，例如火力模块，就有驾驶仓，左机枪，右机枪，火箭发射器，扫描瞄准仪等机器零件组成
	 * 这些零件对于机器模块的相对位置是固定的
	 */
	public class RobotModule 
	{
		/**
		 * 模块名称
		 */
		public var Name:String;
		/**
		 * 朝向
		 */
		public var Heading:Vector2D;
		/**
		 * 零件字典
		 */
		public var PartDict:Dictionary;
		/**
		 * 模块中心位置
		 */
		public var Center:Point;
		/**
		 * 容器
		 */
		public var Canvas:Sprite;
		/**
		 * 零件主板
		 */
		public var PartBoard:Sprite;
		
		public function RobotModule(_name:String,_x:int,_y:int,_defaultHeading:Number = 0,_scale:Number = 1.0)
		{
			Name = _name;
			Canvas = new Sprite();
			PartBoard = new Sprite();
			Canvas.addChild(PartBoard);
			PartDict = new Dictionary();
			PartBoard.x = _x;
			PartBoard.y = _y;
			Canvas.rotation = _defaultHeading;
			var shape:Shape = new Shape();
			shape.graphics.beginFill(0x00ff00, 1);
			shape.graphics.drawCircle(0, 0, 2);
			Canvas.addChild(shape);
			PartBoard.scaleX = _scale;
			PartBoard.scaleY = _scale;
			Heading = new Vector2D(1, 1);
			Heading.Angle = _defaultHeading * Math.PI / 180;
			
			Canvas.addEventListener(Event.ENTER_FRAME, function(e:Event):void
			{
				Update();	
			});
		}
		/**
		 * 添加零件
		 */
		public function AddPart(_part:RobotPart,_pos:Point,_angle:Number = 0):void
		{
			PartBoard.addChild(_part.Canvas);
			_part.Canvas.x = _pos.x;
			_part.Canvas.y = _pos.y;
			PartDict[_part.Name] = _part;
			_part.Center = _pos;
		}
		/**
		 * 卸载零件
		 */
		public function RemovePart(_partName:String):void
		{
			var part:RobotPart = PartDict[_partName];
			if (part != null)
			{
			   PartBoard.removeChild(part.Canvas);
			   part.Dispose();
			   this.PartDict[_partName] = null;
			}
		}
		private function Update():void
		{
			Canvas.rotation = Heading.Angle * 180 / Math.PI;		
		}
	}
}